import AdItem from "./Items/AdItem";
import MathTool from "../../Tool/MathTool";
import { ui } from "../../../../ui/layaMaxUI";
import { BaseAdView } from "./BaseAdView";
import { AdConfigData } from "./ShiyouAdManager";

export default class FightOverAdView extends ui.ShiyouAdView.FightOverAdViewUI implements BaseAdView {

    constructor(){
        super(); 
        Laya.stage.addChild(this);
    }

    private m_dataArray:Array<AdConfigData>=[];
    private m_indexarray:Array<number>=[];
    private m_lefthasinit:boolean=false;
    private m_righthasinit:boolean=false;

    createChildren(){
        super.createChildren();
        this.m_leftList.renderHandler=new Laya.Handler(this,this.OnLeftListRender);
        this.m_rightList.renderHandler=new Laya.Handler(this,this.OnRightListRender);
    }


    InitData(gameadsData: Array<AdConfigData>){

        if (gameadsData && gameadsData.length < 1) {
            return;
        }

        if (this.m_dataArray.length<1) 
        {
            for (let index = 0; index < gameadsData.length; index++) {
                const element = gameadsData[index];
                if (element.Type5 !=null) {
                    this.m_dataArray.push(element);
                }
            }
        }            
        if (this.m_dataArray.length < 1) {
            return;
        } 
        
        for (let index = 0; index < this.m_dataArray.length; index++) {
           this.m_indexarray.push(index);
        }

        let leftarray=[];
        for (let index = 0; index < 5; index++) {
            if (this.m_indexarray.length<1) {
                leftarray.push(this.m_dataArray[MathTool.GetRandomInt(0,this.m_dataArray.length)]);
            }
            else{
                let leftindex=MathTool.GetRandomInt(0, this.m_indexarray.length);
                leftarray.push(this.m_dataArray[this.m_indexarray[leftindex]]);
                this.m_indexarray.splice(leftindex,1);
            }
        }

        let rightarray=[]; 
        for (let index = 0; index < 5; index++) {
            if (this.m_indexarray.length<1) {
                rightarray.push(this.m_dataArray[MathTool.GetRandomInt(0,this.m_dataArray.length)]);
            }
            else{
                let rightindex=MathTool.GetRandomInt(0, this.m_indexarray.length);
                rightarray.push(this.m_dataArray[this.m_indexarray[rightindex]]);
                this.m_indexarray.splice(rightindex,1);
            }
        }

        leftarray.sort(this.Sort.bind(this));
        rightarray.sort(this.Sort.bind(this));

        this.m_leftList.array=leftarray;
        this.m_rightList.array=rightarray;
        this.m_indexarray.length=0;
    }


   private Sort(a,b){
        if (a.Type5 < b.Type5)return -1;
        if (a.Type5 == b.Type5)return 0;   
        return 1;
    }

    OnLeftListRender(cell:AdItem,index:number){
        cell.SetData(cell.dataSource,"Type5",cell.dataSource.Type5,true);
        cell.ShowPosAm();
    }

    OnRightListRender(cell:AdItem,index:number){
        cell.SetData(cell.dataSource,"Type5",cell.dataSource.Type5,true);
        cell.ShowPosAm();
    }



    Show(top:number) 
    {
        this.visible=true;
        if (this.m_dataArray.length>0) {
            this.InitData(this.m_dataArray);
        }
       
    }

    Hide() {
        Laya.timer.clearAll(this);
        this.visible = false;
    }


}